<?php
require_once("includes/init.inc.php");
require_once("includes/api.files.php");

$to = $_POST["emailText"];
$message = stripcslashes($_POST["message"]);
$subject = "File sent from SafeCube";

//$headers  = 'MIME-Version: 1.0' . "\r\n";
//$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWD) or die(mysql_error());
mysql_select_db(DB_NAME) or die(mysql_error());

// get file ID
$fileId = $_POST["fileId"];

$fileInfo = get_file_info($fileId);
$fileName = $fileInfo["name"];
$fileDBName = $fileInfo["file"];
$extension = $fileInfo["ext"];
$contentType = $fileInfo["contentType"];

$fileatt = "files/{$fileDBName}.{$extension}";
$fileatttype = $contentType;
$fileattname = "{$fileName}.{$extension}";

	$file = "files/{$fileDBName}.{$extension}";
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
	$uid = md5(uniqid(time()));
	
	$header = "From: SafeCube <safecube@byhumans.com>\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= "Attached file sent from SafeCube"."\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: {$fileatttype}; name=\"".$fileattname."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$fileattname."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";


$test = mail ( $to , $subject , "", $header );

if($test){
	header('Location: main.php?success=true');
	die();
}
//else 
	//echo "fail";
// bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

	
	
	